package edu.tsinghua.mrpvc.device.camera;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;

import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventHandler;
import org.osgi.util.tracker.ServiceTracker;

import edu.tsinghua.mrpvc.device.CameraDevice;
import edu.tsinghua.mrpvc.device.simulate.CameraSimulateTopic;


/**
 * 
 * @author 
 * Last updated on 2009-11-24 by Tangjian
 */
public class CameraDeviceImpl implements CameraDevice, EventHandler{
	//private BundleContext context;
	private boolean valid;
	private ServiceTracker tracker;
	private String ID;
	
	public CameraDeviceImpl(BundleContext context, String ID){
		this.ID = new String(ID);
		valid = true;
		tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
		tracker.open();		
	}

	@Override
	public boolean isValid() {
		// TODO Auto-generated method stub
		return valid;
	}

	@Override
	public String getID() {
		// TODO Auto-generated method stub
		return ID;
	}
	
	private void generEvent(){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			Map<String, String> property = new Hashtable<String, String>();
			property.put(CameraDevice.EVENT_PROPER_ISVALID, valid == true ? CameraDevice.Constants.ISVALID_TURE : CameraDevice.Constants.ISVALID_FALSE);
			property.put(CameraDevice.EVENT_PROPER_ID, ID);
			Event event = new Event(CameraDevice.EVENT_TOPIC_STATE, property);
			eventAdmin.postEvent(event);
		}
		
	}
	
	private void generEventOfTv(String action){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			System.out.println(Calendar.getInstance().getTime().toLocaleString() +
			" Camera: recognise owener's gesture and post event");
			Map<String, String> property = new Hashtable<String, String>();
			//property.put(CameraDevice.EVENT_PROPER_ISVALID, valid == true ? "true" : "false");
			//property.put(CameraDevice.EVENT_PROPER_ID, ID);
			property.put(CameraDevice.EVENT_PROPER_ACTION, action);
			Event event = new Event(CameraDevice.EVENT_TOPIC_CTRL_TV, property);
			eventAdmin.postEvent(event);
		}
	}
	
	private void generEventOfCurtain(String direction){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			System.out.println(Calendar.getInstance().getTime().toLocaleString() +
					" Camera: recognise owener's gesture and post event");
			Map<String, String> property = new Hashtable<String, String>();
			//property.put(CameraDevice.EVENT_PROPER_ISVALID, valid == true ? "true" : "false");
			//property.put(CameraDevice.EVENT_PROPER_ID, ID);
			property.put(CameraDevice.EVENT_PROPER_DIRECTION, direction);
			Event event = new Event(CameraDevice.EVENT_TOPIC_CTRL_CURTAIN, property);
			eventAdmin.postEvent(event);
		}
	}
	
	public void setActionOfTv(String action){
		if(action.equals(Constants.ACTION_BIGGER)){
			generEventOfTv(action);
		}
		else if(action.equals(Constants.ACTION_NEXT)){
			generEventOfTv(action);
		}
		else if(action.equals(Constants.ACTION_PREV)){
			generEventOfTv(action);
		}
		else if(action.equals(Constants.ACTION_ROTATE)){
			generEventOfTv(action);
		}
		else if(action.equals(Constants.ACTION_SMALLER)){
			generEventOfTv(action);
		}
	}
	
	public void setDirectionOfCurtain(String direction){
		if(direction.equals(Constants.DIR_WEST) || direction.equals(Constants.DIR_EAST) ||
				direction.equals(Constants.DIR_NORTH) || direction.equals(Constants.DIR_SOUTH)){
			generEventOfCurtain(direction);
		}
		
	}

	@Override
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
		if(event.getTopic().equals(CameraDevice.EVENT_TOPIC_ISVALID)){
			if(event.getProperty(CameraDevice.EVENT_PROPER_ID) != null && event.getProperty(CameraDevice.EVENT_PROPER_ID).equals(ID)){
				generEvent();
			}
		}
		else if(event.getTopic().equals(CameraSimulateTopic.CAMERA_CTRL_CURTAIN) && 
				event.getProperty(CameraSimulateTopic.PROPER_DIRECTION) != null){
			String direction = event.getProperty(CameraSimulateTopic.PROPER_DIRECTION).toString();
			setDirectionOfCurtain(direction);
		}
		else if(event.getTopic().equals(CameraSimulateTopic.CAMERA_CTRL_TV) &&
			event.getProperty(CameraSimulateTopic.PROPER_ACTION) != null){
			String action = event.getProperty(CameraSimulateTopic.PROPER_ACTION).toString();
			setActionOfTv(action);
		}		
	}
}
